<?php
/**
 * 画种导购
 */

define('IN_ECS', true);
require (dirname(__FILE__) . '/includes/init.php');

$act = $_REQUEST['act'];

/* 获得首部轮转图 */
if($act === 'banner'){
	$guideBanner = array('url1' ,
		                 'url2', 
		                 'url3');
	$response = array('response'=>'guideBanner', 'guideBanner'=>$guideBanner);
	echo json_encode($response);
}
/* 种类详情请求 */
else if($act === 'guide'){

	$allow_cat_id = array(197, 199, 200, 201, 203, 204, 205);

	/* 获得请求的分类 ID */
    if (isset($_REQUEST['cat_id']) && in_array($_REQUEST['cat_id'], $allow_cat_id)){
        $cat_id = intval($_REQUEST['cat_id']);
    }
    else{
        $cat_id = 197;
    }

	if($cat_id==197){
        $goods_list = category_get_goods(get_children($cat_id),  "10000", "1", "sort_order","ASC");
    }else{
        $goods_list = category_get_goods(get_children($cat_id),  "10000", "1", "goods_id","ASC");
    }

	$response = array('response'=>'guideBuy', 'goodslist'=>$goods_list);
 	echo tourldecode($response);
}


/**
 * 获得分类下的商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($children, $pagesize, $page, $sort, $order)
{
    
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.click_count, g.shop_price, g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb , g.goods_img, g.goods_type,' .
                "(select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " WHERE goods_id = g.goods_id ) AS buy_sum ".
                'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
                "WHERE $where ORDER BY $sort $order";

    $res = $GLOBALS['db']->selectLimit($sql, $pagesize, ($page - 1) * $pagesize);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['id']         = $row['goods_id'];
        $arr[$row['goods_id']]['name']       = urlencode($row['goods_name']);
        $arr[$row['goods_id']]['clickCount']      = $row['click_count'];
        $arr[$row['goods_id']]['marketPrice']     = $row['market_price']!=0 ? $row['market_price'] : (string)($row["shop_price"]*1.2);
        $arr[$row['goods_id']]['shopPrice']       = $row['shop_price'];
        //$arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promotePrice']    = (string)$promote_price;
        $arr[$row['goods_id']]['picture']      = urldecode($_SERVER['HTTP_HOST'] . '/' . $row['goods_thumb']);   //小图
        //$arr[$row['goods_id']]['goods_img']        = $row['goods_img'];    //中图
        //$arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['buySum']          = $row['buy_sum'];
        //$arr[$row['goods_id']]['thumb_list']       = get_thumb($row['goods_id']);
        
        $return[] = $arr[$row['goods_id']];          //去掉数组索引
    }
    

    return $return;
}


?>